package com.example.sprintboot.controller;

import com.example.sprintboot.entity.Category;
import com.example.sprintboot.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/categories")
@PreAuthorize("hasRole('ADMIN')")
public class AdminCategoryController {

    @Autowired
    private CategoryService categoryService;

    // 获取分类树
    @GetMapping
    public Map<String, Object> getCategoryTree() {
        try {
            List<Category> categories = categoryService.getCategoryTree();
            return success("查询成功", categories);
        } catch (Exception e) {
            return error("查询失败: " + e.getMessage());
        }
    }

    // 获取所有分类
    @GetMapping("/list")
    public Map<String, Object> getCategoryList() {
        try {
            List<Category> categories = categoryService.getAllCategories();
            return success("查询成功", categories);
        } catch (Exception e) {
            return error("查询失败: " + e.getMessage());
        }
    }

    // 获取单个分类详情
    @GetMapping("/{categoryId}")
    public Map<String, Object> getCategoryById(@PathVariable Integer categoryId) {
        try {
            Category category = categoryService.getCategoryById(categoryId);
            if (category == null) {
                return error("分类不存在");
            }
            return success("查询成功", category);
        } catch (Exception e) {
            return error("查询失败: " + e.getMessage());
        }
    }

    // 添加分类
    @PostMapping
    public Map<String, Object> addCategory(@RequestBody Category category) {
        try {
            boolean success = categoryService.addCategory(category);
            if (success) {
                return success("添加分类成功");
            } else {
                return error("添加分类失败");
            }
        } catch (Exception e) {
            return error("添加分类失败: " + e.getMessage());
        }
    }

    // 更新分类
    @PutMapping("/{categoryId}")
    public Map<String, Object> updateCategory(@PathVariable Integer categoryId, @RequestBody Category category) {
        try {
            category.setCategoryId(categoryId);
            boolean success = categoryService.updateCategory(category);
            if (success) {
                return success("更新分类成功");
            } else {
                return error("更新分类失败");
            }
        } catch (Exception e) {
            return error("更新分类失败: " + e.getMessage());
        }
    }

    // 删除分类
    @DeleteMapping("/{categoryId}")
    public Map<String, Object> deleteCategory(@PathVariable Integer categoryId) {
        try {
            boolean success = categoryService.deleteCategory(categoryId);
            if (success) {
                return success("删除分类成功");
            } else {
                return error("删除分类失败，该分类下可能有商品或子分类");
            }
        } catch (Exception e) {
            return error("删除分类失败: " + e.getMessage());
        }
    }

    // 获取父分类选项（用于添加子分类时选择）
    @GetMapping("/parents")
    public Map<String, Object> getParentCategories() {
        try {
            List<Category> parentCategories = categoryService.getParentCategories();
            return success("查询成功", parentCategories);
        } catch (Exception e) {
            return error("查询失败: " + e.getMessage());
        }
    }

    // 工具方法
    private Map<String, Object> success(String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", message);
        return response;
    }

    private Map<String, Object> success(String message, Object data) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", message);
        response.put("data", data);
        return response;
    }

    private Map<String, Object> error(String message) {
        Map<String, Object> response = new HashMap<>();
        response.put("success", false);
        response.put("message", message);
        return response;
    }
} 